﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.CanGongService;

namespace CommunitySystem
{
    public partial class CanGongDetailWindow : ChildWindow
    {
        private CanGongDetail model = new CanGongDetail();
        public CanGongService.CanGongServiceClient client = new CanGongServiceClient();
        public int ID;
        public bool IsNew = false;
        public int OrgID;
        public CanGongDetailWindow(int id, bool isnew,int orgid)
        {
            InitializeComponent();
            ID = id;
            IsNew = isnew;
            OrgID = orgid;
            client.GetCanGongDetailModelCompleted += new EventHandler<GetCanGongDetailModelCompletedEventArgs>(client_GetCanGongDetailModelCompleted);
            client.InsertCheGaiDetailCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_InsertCheGaiDetailCompleted);
            client.UpdateCheGaiDetailCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_UpdateCheGaiDetailCompleted);
            client.UpdateCheGaiDetailStatusCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_UpdateCheGaiDetailStatusCompleted);
            if (IsNew)
            {
                model = new CanGongDetail();
                this.DataContext = model;
            }
            else
                client.GetCanGongDetailModelAsync(0, 0, ID);
        }

        void client_UpdateCheGaiDetailStatusCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
            //result();
            this.DialogResult = true;
        }

        void client_UpdateCheGaiDetailCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("提交成功！");
            //result();
            this.DialogResult = true;
        }

        void client_InsertCheGaiDetailCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
            //result();
            this.DialogResult = true;
        }

        void client_GetCanGongDetailModelCompleted(object sender, GetCanGongDetailModelCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Count > 0)
            {
                model = e.Result[0];
                this.DataContext = model;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            model.OrgID = OrgID;
            if (string.IsNullOrEmpty(model.Name))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请填写人员姓名！");
                return;
            }
            if (string.IsNullOrEmpty(model.CardNO) )
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请填写证件号码！");
                return;
            }

            if (IsNew)
            {
                client.InsertCheGaiDetailAsync(model);
            }
            else
                client.UpdateCheGaiDetailAsync(model);
        }
    }
}
